package com.ce.source.analysis.lifecycle.spring.code;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

/**
 * @author ：前世小和尚（xiaopingzhang8@creditease.cn）
 * @Description ：代码演示
 * @ClassName ：Person
 * @Company ：普信恒业科技发展（北京）有限公司
 * @date ：2018/1/29 13:42
 */
public class Person implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {
    private static final Logger log = LoggerFactory.getLogger(Person.class);
    private String name;
    private int phone;
    private BeanFactory beanFactory;
    private String beanName;


    public Person() {
        log.info("调用person无参构造器");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        log.info("[注入属性]name");
        this.name = name;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        log.info("[注入属性]phone");
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", phone=" + phone +
                '}';
    }

    /**
     * Callback that supplies the owning factory to a bean instance.
     * <p>Invoked after the population of normal bean properties
     * but before an initialization callback such as
     * {@link InitializingBean#afterPropertiesSet()} or a custom init-method.
     *
     * @param beanFactory owning BeanFactory (never {@code null}).
     *                    The bean can immediately call methods on the factory.
     * @throws BeansException in case of initialization errors
     * @see BeanInitializationException
     */
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        log.info("[BeanFactoryAware]调用setBeanFactory()");
        this.beanFactory = beanFactory;

    }

    /**
     * Set the name of the bean in the bean factory that created this bean.
     * <p>Invoked after population of normal bean properties but before an
     * init callback such as {@link InitializingBean#afterPropertiesSet()}
     * or a custom init-method.
     *
     * @param name the name of the bean in the factory.
     *             Note that this name is the actual bean name used in the factory, which may
     *             differ from the originally specified name: in particular for inner bean
     *             names, the actual bean name might have been made unique through appending
     *             "#..." suffixes. Use the {@link BeanFactoryUtils#originalBeanName(String)}
     *             method to extract the original bean name (without suffix), if desired.
     */
    public void setBeanName(String name) {
        log.info("[BeanNameAware]调用setBeanName()");
        this.beanName = name;

    }

    /**
     * Invoked by a BeanFactory on destruction of a singleton.
     *
     * @throws Exception in case of shutdown errors.
     *                   Exceptions will get logged but not rethrown to allow
     *                   other beans to release their resources too.
     */
    public void destroy() throws Exception {
        log.info("[DisposableBean]调用destroy()销毁");

    }

    /**
     * Invoked by a BeanFactory after it has set all bean properties supplied
     * (and satisfied BeanFactoryAware and ApplicationContextAware).
     * <p>This method allows the bean instance to perform initialization only
     * possible when all bean properties have been set and to throw an
     * exception in the event of misconfiguration.
     *
     * @throws Exception in the event of misconfiguration (such
     *                   as failure to set an essential property) or if initialization fails.
     */
    public void afterPropertiesSet() throws Exception {
        log.info("[InitializingBean]调用afterPropertiesSet设置属性");
    }

    public void myInit() {
        log.info("手动初始化init-method");
    }

    public void myDestory() {
        log.info("手动销毁destroy-method");
    }
}
